package com.atguigu.gulimall.order.thread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description
 * @Author lhj
 * @Date 2024/5/2 10:29
 */
@Configuration
public class ThreadPoolConfiguration {
    
    @Autowired
    ThreadPoolProperties threadPoolProperties;
    
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        poolTaskExecutor.initialize();
        poolTaskExecutor.setCorePoolSize(threadPoolProperties.getCoreSize());
        poolTaskExecutor.setMaxPoolSize(threadPoolProperties.getMaxSize());
        poolTaskExecutor.setKeepAliveSeconds(threadPoolProperties.getKeepAlive());
        poolTaskExecutor.setQueueCapacity(threadPoolProperties.getQueueSize());
        poolTaskExecutor.setThreadNamePrefix("order-");
        poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        return poolTaskExecutor;
    }
    
}
